// -----------------------------------------------------------------------------
//
// Work is licensed by Arthur Fermi (C)2008-2011,
// under Creative Commons Attribution-Share Alike 3.0 Unported License
// To receive a copy of this license please visit
// http://creativecommons.org/licenses/by-sa/3.0/
//
// You are free to copy, distribute, transmit and adapt the work
// under the following conditions:
//
// You must attribute the work in the manner specified by the 
// author(s) or licensor (but not in any way that suggests that 
// they endorse you or your use of the work). 
//
// If you alter, transform, or build upon this work in any way,
// you may distribute the resulting work only under the same or 
// similar license to this one. 
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See points (6) and (7) of the license for more details.
//
// -----------------------------------------------------------------------------
// Do not change anything below here or you will likely break something
// -----------------------------------------------------------------------------
// compile with lsl2
// -----------------------------------------------------------------------------

// SimNet - Messages

// Global Variables
list    glstMessages;       // Clean Message List
integer gintDCC;            // Dialog Comm Channel
list    glstMenu;           // Dialog Menu
string  gstrDialogText;     // Dialog Text

// Read in messages
string  gstrURL;
string  gstrHTTP;

funSetupMenu(string menu)
{
    // Convert body to temporary string
    list lstTempList = llParseString2List(menu,["^"],[""]); 
    
    // Get list length
    integer intListLen = llGetListLength(lstTempList);

    
    
    integer i;
    for (i = 0; i < intListLen; i++)
    {
        if(i%2 == 0)
        {
            glstMenu += llList2String(lstTempList, i);
        }
        else
        {
            glstMessages += llList2String(lstTempList, i);
         }
              
    }
    glstMenu += ["ReloadMSG"];
}

default
{
    state_entry()
    {
        // Clear list
        glstMessages = [];
        glstMenu = [];

        // Get URL for web site
        llMessageLinked(LINK_SET, 333, "Getting URL", NULL_KEY);
                                
        // Dialog Text
        gstrDialogText = "Please select the message you want to shout out.  The message will be heard by everyone with in 96m of you.";
        
        // Setup up communications channel and listen
        gintDCC = llCeil(llFrand(10000.0));
        llListen(gintDCC, "", llGetOwner(), "" );

        // Get URL for web site
        llMessageLinked(LINK_SET, 333, "Getting URL", NULL_KEY);
        
        // Set Name
        llSetObjectName("SimNet Toolbar Messages");
        
        llOwnerSay("Messages loaded");

    }

    touch_start(integer total_number)
    {
        llDialog(llDetectedKey(0),gstrDialogText,glstMenu,gintDCC);
    }
    
    link_message(integer sender_num, integer num, string str, key id)
    {
        if ( num == 334)
        {
            gstrURL = str;
            
            // Get Messages
            gstrHTTP = llHTTPRequest(gstrURL + "simnet_messages.msg",[HTTP_METHOD, "POST"], "");           
        }
    }
    
    listen(integer channel, string name, key id, string message)
    {
        if (channel == gintDCC)
        {
            if (message == "ReloadMSG")
            {
                llOwnerSay("Reloading messages from the web");
                llResetScript();
            }
            else
            {
                llSetObjectName(llKey2Name(llGetOwner()));
                string strMsg = llList2String(glstMessages,llListFindList(glstMenu,[message]));
                llShout(0,strMsg);
            }
        }
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        
        if (request_id == gstrHTTP)
        {
            request_id = NULL_KEY;
            funSetupMenu(body);
        }
    }
    
    on_rez(integer start_param)
    {
        llResetScript();
    }
}